package jianli;
import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;

/**
 * Servlet Filter implementation class filterThree
 */
@WebFilter(
		dispatcherTypes = {
				DispatcherType.FORWARD, 
				DispatcherType.INCLUDE, 
				DispatcherType.ERROR
		},filterName = "loginFilter", initParams ={@WebInitParam(name = "过滤器功能", value = "防止中文乱码", description = "UTF-8编码"),
@WebInitParam(name = "过滤器设计者", value = "老王", description = "记录过滤器设计者") },
urlPatterns = {"/loginFilter","/*"})
public class loginFilter implements Filter {
	/**
     * Default constructor. 
     */
    public loginFilter() {
        // TODO Auto-generated constructor stub
    }
	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		System.out.println("过滤器动作：设置UTF-8编码");
		request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        chain.doFilter(request, response);
        return ;
	}
	public void init(FilterConfig fConfig) throws ServletException
    {
			System.out.println("过滤器名字："+fConfig.getFilterName());//过滤器名字	
			System.out.println("过滤器初始化方法参数：");
			Enumeration<String> enums = fConfig.getInitParameterNames();//枚举量用于获得过滤器初始化参数
			while(enums.hasMoreElements()){//循环获得参数
				String paramName = enums.nextElement();
			    String paramValue = fConfig.getInitParameter(paramName);
			    System.out.println(paramName + "-" + paramValue);
			}
			ServletContext fcontext = fConfig.getServletContext();//上下文
			System.out.println(fcontext.toString());
		}

	@Override
    public void destroy()
    {
		System.out.println("过滤器销毁！");

    }

}
